package com.doctor.adapter;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.doctor.bean.WorkArrangeBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.DialogHelper;
import com.doctor.comm.DialogHelper.DialogSelectCallback;
import com.doctor.database.DbOperator;
import com.doctor.ui.R;

import android.content.ContentValues;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class CalendarAdapter extends BaseAdapter{

	/**
	 * 日历控件adapter
	 */
	
	private Context context;
	private List<String> beanList;
	private AbsListView.LayoutParams params;
	private int lines;
	private LongClick longClick;
	private String[] arrange={"无","行政班","中班","夜班","休息","门诊","其它","取消"};
	private Map<Integer, WorkArrangeBean> map;
	private ContentValues values;
	private Calendar calendar;
	private Calendar nowCalendar;  //系统日期
	private boolean isNow;
	
	public CalendarAdapter(Context context,final GridView gridView,Calendar calendar,List<String> beanList,final int lines,boolean isNow){
		this.context=context;
		this.beanList=beanList;
		this.lines=lines;
		longClick=new LongClick();
		this.calendar=calendar;
		this.isNow=isNow;
		map=DbOperator.getInstance(context).selectWorkArrangeData(calendar.get(Calendar.YEAR), (calendar.get(Calendar.MONTH)+1));
		values=new ContentValues();
		params=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		gridView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
			@Override
			public void onGlobalLayout() {
				// TODO Auto-generated method stub
				int height=gridView.getHeight();
				params.height=(height-(lines-1)-12)/lines;
				gridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				notifyDataSetChanged();
			}
		});
		nowCalendar=Calendar.getInstance();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beanList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		String date_txt=beanList.get(arg0);
		
		holder=new ViewHolder();
		arg1=View.inflate(context, R.layout.calendarview_item, null);
		holder.date=(TextView) arg1.findViewById(R.id.date);
		holder.mark=(TextView) arg1.findViewById(R.id.mark);
		
		holder.date.setText(date_txt);
		
		arg1.setLayoutParams(params);
		arg1.setTag(R.id.date_tag, date_txt);
		
		if (date_txt!=null && !"".equals(date_txt)) {
			if (map!=null && map.size()>0 && map.containsKey(Integer.valueOf(date_txt))) {
				WorkArrangeBean bean=map.get(Integer.valueOf(date_txt));
				holder.mark.setText(bean.getArrange());
				holder.mark.setVisibility(View.VISIBLE);
			} else {
				holder.mark.setText("");
				holder.mark.setVisibility(View.GONE);
			}
			arg1.setOnLongClickListener(longClick);
			
			if (isNow && nowCalendar.get(Calendar.DAY_OF_MONTH)==Integer.parseInt(date_txt)) {
				//当天用红色标注
				holder.mark.setTextColor(Color.parseColor("#FF0000"));
				holder.date.setTextColor(Color.parseColor("#FF0000"));
				arg1.setBackgroundResource(R.color.gray_green);
			} else {
				holder.mark.setTextColor(Color.parseColor("#3baeda"));
				holder.date.setTextColor(Color.parseColor("#666666"));
				arg1.setBackgroundResource(R.drawable.activitybg_to_graygreen);
			}
		} else {
			holder.mark.setText("");
			holder.mark.setVisibility(View.GONE);
		}
		
		return arg1;
	}

	
	private class ViewHolder{
		TextView date;
		TextView mark;
	}
	
	private class LongClick implements OnLongClickListener{
		@Override
		public boolean onLongClick(final View v) {
			// TODO Auto-generated method stub
			final TextView mark=(TextView) v.findViewById(R.id.mark);
			final String date_txt=(String) v.getTag(R.id.date_tag);
			if (date_txt==null || "".equals(date_txt)) {
				//空白则不弹出对话框
				return false;
			}
			DialogHelper.selectDialog(context, "选择类型", arrange, new DialogSelectCallback() {				
				@Override
				public void onDialogSelected(int position) {
					// TODO Auto-generated method stub
					int day=Integer.valueOf((String) v.getTag(R.id.date_tag));
					if (position==0) {
						//设置无
						if (map.containsKey(day)) {
							//之前有工作安排则删除,更新adapter数据
							int row=DbOperator.getInstance(context).deleteData(ConstConfig.WORK_ARRANGE_TABLE,map.get(day).getId());
							if (row>0) {
								map.remove(day);
//								Toast.makeText(context, "删除成功!", Toast.LENGTH_SHORT).show();
							}
							notifyDataSetChanged();
						}				
					} else if(position!=arrange.length-1) {
						//设置工作安排
						values.clear();
						String year=calendar.get(Calendar.YEAR)+"";
						String month=(calendar.get(Calendar.MONTH)+1)+"";						
						values.put("_date",year+"-"+month+"-"+date_txt);
						values.put("_arrange", arrange[position]);
						values.put("_year", year);
						values.put("_month", month);
						values.put("_day", day);
						if (!map.containsKey(day)) {
							//原先没有工作安排则插入数据,更新adapter
							long row=DbOperator.getInstance(context).insertData(ConstConfig.WORK_ARRANGE_TABLE, values);
							if (row>0) {
								map=null;
								map=DbOperator.getInstance(context).selectWorkArrangeData(calendar.get(Calendar.YEAR), (calendar.get(Calendar.MONTH)+1));
								notifyDataSetChanged();
//								Toast.makeText(context, "设置成功！", Toast.LENGTH_SHORT).show();
							} else {
//								Toast.makeText(context, "设置失败！", Toast.LENGTH_SHORT).show();
							}
						} else {
							//原先有工作安排则更新数据库表
							long row=DbOperator.getInstance(context).updateData(ConstConfig.WORK_ARRANGE_TABLE, values, map.get(day).getId());
							if (row>0) {
								WorkArrangeBean bean=map.get(day);
								bean.setArrange(arrange[position]);
								map.put(day, bean);
								notifyDataSetChanged();
//								Toast.makeText(context, "修改成功！", Toast.LENGTH_SHORT).show();
							} else {
//								Toast.makeText(context, "修改失败！", Toast.LENGTH_SHORT).show();
							}
						}
					}					
				}
			});
			return false;
		}		
	}
	
	
}
